package cn.com.controller.operationController;


import cn.com.Enum.UserAuth;
import cn.com.annotation.Auth;
import cn.com.constant.Code;
import cn.com.db.model.Parts;
import cn.com.db.model.StoreParts;
import cn.com.db.service.PartsService;
import cn.com.db.service.StorePartsService;
import cn.com.util.DataResult;
import cn.com.util.JsonUtil;
import cn.com.util.TimeUtil;
import cn.com.util.UidUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;


@Auth(UserAuth.SELLER)
@RestController
@RequestMapping("/operation/parts")
public class PartsOperationController {

    @Autowired
    PartsService partsService;
    @Autowired
    StorePartsService storePartsService;

    @RequestMapping(method = RequestMethod.POST)
    public String getParts(@RequestParam(value = "parent") String parent,
                             @RequestParam("storeId")String storeId){
        DataResult dataResult=new DataResult();
        dataResult.setData(storePartsService.getStoreParts(storeId,parent));
        dataResult.setTotal(storePartsService.countStoreParts(storeId,parent));
        dataResult.setCode(Code.SUCCESS);
        dataResult.setMessage("返回成功");
        return JsonUtil.toJsonString(dataResult);
    }

    @RequestMapping(value = "/getParts",method = RequestMethod.POST)
    public String getParts(@RequestParam("parent") String parent){
        DataResult dataResult=new DataResult();
        dataResult.setData(partsService.getParts(parent));
        dataResult.setTotal(partsService.countParts(parent));
        dataResult.setCode(Code.SUCCESS);
        dataResult.setMessage("返回成功");
        return JsonUtil.toJsonString(dataResult);
    }

    @RequestMapping(value = "/onParts",method = RequestMethod.POST)
    public String onParts(@RequestParam("storeId") String storeId,
                            @RequestParam("partsId")String partsId,
                            @RequestParam("price")BigDecimal price){
        DataResult dataResult=new DataResult();
        if (!partsService.isExist(partsId)){
            dataResult.setCode(Code.NOT_FOUND);
            dataResult.setMessage("表层不存在");
            return JsonUtil.toJsonString(dataResult);
        }
        StoreParts storeParts=storePartsService.getStorePartsOfStore(storeId,partsId);
        Parts parts=partsService.getPartsById(partsId);
        if (storeParts==null){
            storeParts=new StoreParts();
            storeParts.setId(UidUtil.createUUID());
            storeParts.setParts(parts);
            storeParts.setStore(storeId);
            storeParts.setDel(false);
            storeParts.setPrice(price);
            storeParts.setParent(parts.getParent());
            storeParts.setCreated_at(TimeUtil.getNowTimestamp());
            storeParts.setUpdated_at(TimeUtil.getNowTimestamp());
            storePartsService.onParts(storeParts);
            dataResult.setData(storePartsService.getStorePartsById(storeParts.getId()));
        }else if (storeParts.getDel()){
            storeParts.setDel(false);
            storeParts.setPrice(price);
            storePartsService.updateParts(storeParts);
            dataResult.setData(storePartsService.getStorePartsById(storeParts.getId()));
        }else {
            dataResult.setCode(Code.SERVER_ERROR);
            dataResult.setMessage("店铺已启用该表层");
            return JsonUtil.toJsonString(dataResult);
        }
        dataResult.setCode(Code.SUCCESS);
        dataResult.setMessage("操作成功");
        return JsonUtil.toJsonString(dataResult);
    }


    @RequestMapping(value = "/offParts",method = RequestMethod.POST)
    public String offParts(@RequestParam("id") String storePartsId){
        DataResult dataResult=new DataResult();
        if (!storePartsService.isAlive(storePartsId)){
            dataResult.setCode(Code.SERVER_ERROR);
            dataResult.setMessage("表层不存在,或表层不处于启用状态");
            return JsonUtil.toJsonString(dataResult);
        }
        StoreParts storeParts= storePartsService.getStorePartsById(storePartsId);
        storeParts.setDel(true);
        storePartsService.updateParts(storeParts);
        dataResult.setData(storeParts);
        dataResult.setCode(Code.SUCCESS);
        dataResult.setMessage("操作成功");
        return JsonUtil.toJsonString(dataResult);
    }

    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public String offParts(@RequestParam("id") String storePartsId,
                             @RequestParam("price")BigDecimal price){
        DataResult dataResult=new DataResult();
        if (!storePartsService.isAlive(storePartsId)){
            dataResult.setCode(Code.SERVER_ERROR);
            dataResult.setMessage("表层不存在,或表层不处于启用状态");
            return JsonUtil.toJsonString(dataResult);
        }
        StoreParts storeParts= storePartsService.getStorePartsById(storePartsId);
        storeParts.setPrice(price);
        storePartsService.updateParts(storeParts);
        dataResult.setData(storeParts);
        dataResult.setCode(Code.SUCCESS);
        dataResult.setMessage("修改成功");
        return JsonUtil.toJsonString(dataResult);
    }

}
